//
//  NetSetViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/7/27.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

struct Qua {
    var color:NSColor!
    var title:String!
}

class NetSetViewController: NSViewController {
    var isSeeping:Bool = false
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, kSet_BasicWidth, 500))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        self.view = view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
    }
    
    func setupUI()  {
        self.view.addSubview(proView)
        proView.snp.makeConstraints { (make) in
            make.width.height.equalTo(300)
            make.centerX.equalToSuperview()
            make.top.equalTo(60)
        }
        
        self.view.addSubview(startBtn)
        startBtn.snp.makeConstraints { (make) in
            make.width.equalTo(90)
            make.top.equalTo(400)
            make.centerX.equalToSuperview()
        }
        
        self.view.addSubview(downLostLab)
        downLostLab.snp.makeConstraints { (make) in
            make.height.equalTo(16)
            make.top.equalTo(proView).offset(100)
            make.left.equalTo(proView).offset(50)
        }
        
        self.view.addSubview(downLostspeed)
        downLostspeed.snp.makeConstraints { (make) in
            make.height.equalTo(40)
            make.top.equalTo(downLostLab.snp.bottom).offset(10)
            make.left.equalTo(downLostLab)
        }
        
        self.view.addSubview(upLostLab)
        upLostLab.snp.makeConstraints { (make) in
            make.height.equalTo(16)
            make.top.equalTo(proView).offset(100)
            make.left.equalTo(proView).offset(170)
        }
        
        self.view.addSubview(upLostspeed)
        upLostspeed.snp.makeConstraints { (make) in
            make.height.equalTo(40)
            make.top.equalTo(upLostLab.snp.bottom).offset(10)
            make.left.equalTo(upLostLab)
        }
        
        self.view.addSubview(qualityLab)
        qualityLab.snp.makeConstraints { (make) in
            make.height.equalTo(20)
            make.bottom.equalTo(proView).offset(-84)
            make.centerX.equalToSuperview().offset(15)
        }
        
        self.view.addSubview(qualityView)
        qualityView.snp.makeConstraints { (make) in
            make.width.height.equalTo(16)
            make.right.equalTo(qualityLab.snp.left).offset(-2)
            make.centerY.equalTo(qualityLab)
        }
    }
    
    lazy var proView: OProgressView = {
        let view = OProgressView.init(frame: CGRect.init(x: 0, y: 0, width: 300, height: 300))
        return view
    }()
    
    lazy var startBtn: NSButton = {
        let btn = NSButton.init(frame: NSRect.zero)
        btn.bezelStyle = .rounded
        btn.title = "开始测试"
        btn.font = NSFont.systemFont(ofSize: 14)
        btn.target = self
        btn.action = #selector(startBtnAction(senber:))
        return btn
    }()
    
    lazy var downLostLab: NSLabel = {
        let lab = NSLabel.label(title: "下行丢包率", textColor: color_gray(200), font: NSFont.systemFont(ofSize: 14))
        lab.sizeToFit()
        return lab
    }()
    
    lazy var downLostspeed: NSLabel = {
        let lab = NSLabel.label(title: "0.0%", textColor: color_gray(40), font: NSFont.systemFont(ofSize: 30))
        lab.sizeToFit()
        return lab
    }()
    
    lazy var upLostLab: NSLabel = {
        let lab = NSLabel.label(title: "上行丢包率", textColor: color_gray(200), font: NSFont.systemFont(ofSize: 14))
        lab.sizeToFit()
        return lab
    }()
    
    lazy var upLostspeed: NSLabel = {
        let lab = NSLabel.label(title: "0.0%", textColor: color_gray(40), font: NSFont.systemFont(ofSize: 30))
        lab.sizeToFit()
        return lab
    }()
    
    lazy var qualityLab: NSLabel = {
        let lab = NSLabel.label(title: "网速状态（未知）", textColor: color_gray(40), font: NSFont.systemFont(ofSize: 14))
        lab.sizeToFit()
        return lab
    }()
    
    lazy var qualityView: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(180).cgColor
        return view
    }()
    
    
    var quality:TRTCQuality = .unknown {
        didSet {
            let colors:[Qua] = [
                Qua.init(color: color_gray(180), title: "未知"),
                Qua.init(color: color_green190, title: "最好"),
                Qua.init(color: color_green200, title: "好"),
                Qua.init(color: NSColor.yellow, title: "一般"),
                Qua.init(color: color_red85, title: "差"),
                Qua.init(color: color_red85, title: "很差"),
            ]
            self.qualityView.layer?.backgroundColor = colors[quality.rawValue].color.cgColor
            self.qualityLab.text = "网速状态("+colors[quality.rawValue].title+")"
        }
    }
    
    
    @objc func startBtnAction(senber:NSButton)  {
        guard let userid = Td.sh().params.userId,let userSig = Td.sh().params.userSig else {
            show_msg("缺少userid和userSig")
            return
        }
        
        self.isSeeping = !self.isSeeping
        senber.title = isSeeping ? "重新测试":"开始测试"
        if isSeeping  {
            senber.isEnabled = false
            senber.title = "正在测试中"
            var downResult:Float = 0
            var upResult:Float = 0
            var quaResult:TRTCQuality = .unknown
            TRTCCloud.sharedInstance()?.startSpeedTest(UInt32(kTencent_Live_api_id)!, userId: userid, userSig: userSig, completion: { (result, inx, count) in
                guard let res = result  else {
                    return
                }
                let downLost = (res.downLostRate)*100
                let upLost = (res.upLostRate)*100
                let quality = res.quality
                let stringLost = String.init(format: "%.1f", downLost)
                let stringupLost = String.init(format: "%.1f", upLost)
                self.downLostspeed.text = stringLost+"%"
                self.upLostspeed.text = stringupLost+"%"
                self.proView.setProgress(Int(100/count)*inx, animated: true)
                self.quality = quality
            
                downResult = downResult<downLost ? downLost:downResult
                upResult = upResult<upLost ? upLost:upResult
                quaResult = quaResult.rawValue<quality.rawValue ? quality:quaResult
                if inx == count {
                    senber.isEnabled = true
                    senber.title = "测试完成"
                    self.proView.setProgress(100, animated: true)
                    let stringLost = String.init(format: "%.1f", downResult)
                    let stringupLost = String.init(format: "%.1f", upResult)
                    self.downLostspeed.text = stringLost+"%"
                    self.upLostspeed.text = stringupLost+"%"
                    self.quality = quaResult
                }
            })
        }else {
            senber.title = "开始测试"
            self.quality = .unknown
            self.downLostspeed.text = "0.0%"
            self.upLostspeed.text = "0.0%"
            self.proView.setProgress(0, animated: true)
            TRTCCloud.sharedInstance()?.stopSpeedTest()
        }
        
    }
}
